<?php

namespace app\api\controller;

use addons\smsbao\library\Smsbao;
use app\common\controller\Api;
use think\Db;
/**
 * 手机短信接口
 */
class Digitalocean extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
    }

    public function droplets_iscustom()
    {
        $id = $this->request->request('id');
        $where = array();
        if (!empty($id)) {
            $where['id'] = $id;
        }
        $iscustom = Db::name('fedition_dropletsurl')
            ->where('status','normal')
            ->where($where)
            ->select();
        if($iscustom){
            foreach ($iscustom as $k => $v) {
                $this->droplets($v);
            }
        }
        $this->success('操作成功');
    }

    public function droplets($iscustom)
    {
        $url = 'https://api.digitalocean.com/v2/droplets';
        $data = $this->geturl($url,$iscustom['token']);
        if($data) {
            $data = json_decode($data, true);
            if (isset($data['droplets'])) {
                if(is_array($data['droplets']) && !empty($data['droplets'])){
                    foreach ($data['droplets'] as $droplet) {
                        if (!empty($droplet['id'])) {
                            $arr['droplets_id'] = $droplet['id'];
                            $arr['name'] = $droplet['name'];
                            $arr['droplets_status'] = $droplet['status'];
                            $arr['ip_address'] = $droplet['networks']['v4'][0]['ip_address'];
                            $arr['tags'] = $droplet['tags'][0];
                            $fedition_droplets = Db::name('fedition_droplets')->where('droplets_id', $arr['droplets_id'])->find();
                            if($fedition_droplets){
                                $arr['updatetime'] = time();
                                Db::name('fedition_droplets')->where('droplets_id', $arr['droplets_id'])->update($arr);
                            }else{
                                $arr['createtime'] = time();
                                $arr['updatetime'] = time();
                                Db::name('fedition_droplets')->insert($arr);
                            }
                        }
                    }
                }
            }
        }
        return true;
    }

    public function test_actions($id = null){
        $token = 'c067124822253dc73d35dbf03d95e538e69164133486c4cea965c8792c6da2b9';
        $droplets_id = '279665987';
        $url = 'https://api.digitalocean.com/v2/droplets/'.$droplets_id.'/actions';
        $data = $this->geturl($url,$token);
        if($data) {
            $data = json_decode($data, true);
            dump($data);
            die;
        }
    }

    public function droplets_actions($id = null)
    {
        if (!empty($id)) {
            $droplets = Db::name('fedition_droplets')
                ->where('id',$id)
                ->find();
        }else {
            $ids = $this->request->request('ids');
            $where = array();
            if (!empty($ids)) {
                $where['id'] = $ids;
            }else{
                $this->error('缺少参数');
            }
            $droplets = Db::name('fedition_droplets')
                ->where($where)
                ->find();
        }
        if (!empty($droplets['droplets_id'])) {
            $dropletsurl = Db::name('fedition_dropletsurl')->where('id', $droplets['dropletsurl'])->find();
            $url = 'https://api.digitalocean.com/v2/droplets/'.$droplets['droplets_id'].'/actions';
//            $datas['type'] = 'power_off';//关机
//            $datas['type'] = 'power_on';//开机
//            $datas['type'] = 'reboot';//重启
            $datas['type'] = 'power_cycle';//类似于按下物理机上的重置按钮，类似于从头开始启动
            $data = $this->posturl($url,$dropletsurl['token'],$datas);
            if($data) {
                $data = json_decode($data, true);
                if (!empty($data['action']['type'])) {
                    $this->success('更新成功',$data['action']['type']);
                }
            }else{
                $this->error('接口错误',$data);
            }
        } else{
            $this->error('未找到服务器');
        }

    }

    function geturl($url,$token){
        $headerArray = array("Accept:application/json","Content-Type:application/json;",'Authorization:Bearer '.$token);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
        $output = curl_exec($ch);
        curl_close($ch);
        return $output;
    }

    public function posturl($url,$token,$data = array()){
        $data = (is_array($data)) ? json_encode($data,JSON_UNESCAPED_UNICODE) : $data;
        $headerArray = array("Accept:application/json","Content-Type:application/json;",'Authorization:Bearer '.$token);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

}
